<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label-width="100px" label="砼单创建时间" prop="startCreateTime">
        <QuickFilterDate :disableArr="[5, 6, 7]" v-model="queryDateArr" />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model.trim="queryParams.orderNo" placeholder="请输入订单编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label-width="60px" label="砼单ID" prop="concreteId">
        <el-input v-model.trim="queryParams.concreteId" placeholder="请输入砼单ID" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工程名称" prop="engineeringName">
        <el-input v-model.trim="queryParams.engineeringName" placeholder="请输入工程名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="teamList" max-height="390">
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="砼单ID" align="center" prop="id" />
      <el-table-column width="200" label="创建时间" align="center" prop="createTime" />

      <el-table-column width="200" label="签收时间" align="center" prop="acceptTime" />

      <el-table-column label="部位名称" align="center" prop="partsName" />
      <el-table-column label="主标号" align="center" prop="primaryDesign" />
      <el-table-column width="200" label="主标号一级品类" align="center" prop="primaryFirstCategory" />
      <el-table-column label="生产方量" align="center" prop="productVolume">
      </el-table-column>
      <el-table-column label="运输方量" align="center" prop="transVolume" />
      <el-table-column label="泵浆方量" align="center" prop="pumpSlurryVolume" />
      <el-table-column label="签收方量" align="center" prop="acceptVolume" />

      <el-table-column label="泵送方量" align="center" prop="pumpingVolume" />

      <el-table-column width="180" label="生产机组" align="center" prop="productUnits">
        <template slot-scope="{row}">
          {{ productUnitsTransform(row.productUnits) }}
        </template>
      </el-table-column>
      <el-table-column label="砼车编号" align="center" prop="concreteCarNo" />

      <el-table-column label="泵车编号" align="center" prop="pumpCarNo" />

      <el-table-column label="砼单状态" align="center" prop="concreteStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.settledStatus == 1">准备生产</span>
          <span v-if="scope.row.settledStatus == 2">搅拌中</span>
          <span v-if="scope.row.settledStatus == 3">待签收</span>
          <span v-if="scope.row.settledStatus == 4">已签收</span>
          <span v-if="scope.row.settledStatus == 5">已作废</span>
        </template>
      </el-table-column>
      <el-table-column label="工程名称" align="center" prop="engineeringName" />
      <el-table-column width="200" label="施工单位名称" align="center" prop="constructionUnit" />
      <el-table-column label="工程运距" align="center" prop="engineeringDistance" />
      <el-table-column label="实际运距" align="center" prop="transDistance" />
      <el-table-column label="结算状态" align="center" prop="settledStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.settledStatus == 0">未计算</span>
          <span v-if="scope.row.settledStatus == 1">已结算</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:team:edit']">时间轴</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog title="砼单时间轴" :visible.sync="timeLineDialog" width="700px" append-to-body :before-close="()=>timeLineDialog = false">
      <div v-for="(item, index) in timeLineData" :key="index">
        <span>{{ item.concreteName }}</span>： <span>{{ item.operateTime }}</span>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  delTeam,
  addTeam,
  updateTeam,
} from "@/api/service/production/scheduling";
import {
  replyConcrete,
} from "@/api/service/production/receiptManagement";
import {
  concreteList,
  getTimeLine,
} from "@/api/service/production/concreteStatistics";
import SelectTeamDialog from "@/components/SelectDialog/SelectTeamDialog";
import QuickFilterDate from "@/components/QuickFilterDate"
import { getDicts } from "@/api/system/dict/data";
export default {
  components: {
    QuickFilterDate,
    SelectTeamDialog,
  },
  name: "Team",
  data () {
    return {
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排班管理表格数据
      teamList: [],
      sysUnitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        startCreateTime: null,
        endCreateTime: null,
        orderNo: null,
        concreteId: null,
        engineeringName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      timeLineDialog:false,
      timeLineData:{}
    };
  },
  computed: {
    queryDateArr: {
      get () {
        if (this.queryParams?.startCreateTime) {
          return [
            this.queryParams?.startCreateTime,
            this.queryParams?.endCreateTime,
          ];
        }
        return [];
      },
      set (value) {
        this.queryParams.startCreateTime = value[0];
        this.queryParams.endCreateTime = value[1];
      },
    },
  },
  created () {
    this.getList();
    this.getDicts()
  },
  methods: {
    productUnitsTransform (value) {
      if (value) {
        const data = value.split(',')
        return data.reduce((pre, cur, index) => {
          const findData = this.sysUnitList.find(item => {
            return item.dictValue === cur
          })
          if (!findData) {
            return value
          }
          return pre + findData.dictLabel + (index < data.length - 1 ? ',' : '')
        },'') 
      }
    },
    async getDicts () {
      const res = await getDicts("sys_unit");
      this.sysUnitList = res.data;
    },
    selectTeam (row) {
      this.form.teamInfo = row;
      this.form.teamId = row.teamId;
    },
    /** 查询排班管理列表 */
    getList () {
      this.loading = true;
      concreteList(this.queryParams).then((response) => {
        this.teamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        sortTeamId: null,
        teamId: null,
        teamInfo: null,
        executeTeamStartTime: null,
        executeTeamEndTime: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParams.startCreateTime = null
      this.queryParams.endCreateTime = null
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.sortTeamId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加排班管理";
    },
    /** 修改按钮操作 */
    async handleUpdate (row) {
      const timeLineEnum = {
        1:'创建时间',
        2:'生产时间',
        3:'出厂时间',
        4:'签收时间',
        5:'作废时间',
        6:'卸货时间',
      }
      const res = await getTimeLine(row.id);
      this.timeLineDialog = true;
      this.timeLineData = res.rows.map((item,index) => {
        return {
          ...item,
          concreteName:timeLineEnum[item.concreteStatus]
        }
      })
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.sortTeamId != null) {
            updateTeam(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeam(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const sortTeamIds = row.sortTeamId || this.ids;
      this.$modal
        .confirm('是否确认删除排班管理编号为"' + sortTeamIds + '"的数据项？')
        .then(function () {
          return delTeam(sortTeamIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download(
        "system/team/export",
        {
          ...this.queryParams,
        },
        `team_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
